package life.mahu.community.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import life.mahu.community.mapper.CategoryMapper;
import life.mahu.community.mapper.QuestionMapper;
import life.mahu.community.mapper.TagMapper;
import life.mahu.community.model.*;
import life.mahu.community.dto.TagDTO;
import life.mahu.community.util.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private QuestionMapper questionMapper;

    @Resource
    private RedisUtil redis;

    private static int ExpireTime = 60*60;   // redis中存储的过期时间s

    /**
     * 获取分类和标签
     * @return
     */
    public List<TagDTO> getTags() {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andIdIsNotNull();
        List<Category> cates = categoryMapper.selectByExample(categoryExample);
        List<TagDTO> tagDTOS = new ArrayList<>();
        for (int i = 0, length = cates.size(); i < length; i++) {
            TagDTO program = new TagDTO();
            int categoryId = cates.get(i).getId();
            program.setCategory(cates.get(i));

            TagExample tagExample = new TagExample();
            tagExample.createCriteria()
                    .andCategoryIdEqualTo(categoryId);

            program.setTags(tagMapper.selectByExample(tagExample));
            tagDTOS.add(program);
        }
        return tagDTOS;
    }

    /**
     * 获取分类
     * @return
     */
    public List<Category> getList() {

        List<Category> categoryList;
        if (redis.hasKey("mahuCategory")) {
            categoryList = (List<Category>) (List) redis.lGet("mahuCategory", 0, -1);
        } else {
            CategoryExample categoryExample = new CategoryExample();
            categoryExample.createCriteria().andIdIsNotNull();
            categoryList = categoryMapper.selectByExample(categoryExample);
            if (categoryList.size() != 0) {
                redis.listRSet("mahuCategory", (List<Object>)(List)categoryList, ExpireTime);
            }
        }
        return categoryList;
    }

    public Boolean delCategoryById(Integer id) {

        TagExample tagExample = new TagExample();
        TagExample.Criteria criteria = tagExample.createCriteria();
        criteria.andCategoryIdEqualTo(id);
        tagMapper.deleteByExample(tagExample);

        int delUser = categoryMapper.deleteByPrimaryKey(id);
        return delUser>0?true:false;
    }

    public PageInfo getCategoryListPage(Category category, Integer page, Integer limit) {
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        if (category.getId() != null && category.getId() != 0) {
            criteria.andIdEqualTo(category.getId());
        }
        if (!(category.getName() == null  || "".equals(category.getName()))) {
            criteria.andNameLike("%" + category.getName() + "%");
        }
        PageHelper.startPage(page, limit);
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        return new PageInfo<Category>(categoryList);
    }

    public Category getCategoryById(Integer id) {
        return categoryMapper.selectByPrimaryKey(id);
    }

    public Boolean insertCategory(Category category) {
        int insert = categoryMapper.insert(category);
        return insert>0?true:false;
    }

    public Boolean updateCategoryById(Category category) {
        int update = categoryMapper.updateByPrimaryKey(category);
        return update>0?true:false;
    }

    public Boolean delTagById(Integer id) {
        int delUser = tagMapper.deleteByPrimaryKey(id);
        return delUser>0?true:false;
    }

    public PageInfo getTagListPage(Tag tag, Integer page, Integer limit) {
        TagExample tagExample = new TagExample();
        TagExample.Criteria criteria = tagExample.createCriteria();
        if (tag.getId() != null && tag.getId() != 0) {
            criteria.andIdEqualTo(tag.getId());
        }
        if (tag.getCategoryId() != null && tag.getCategoryId() != 0) {
            criteria.andCategoryIdEqualTo(tag.getCategoryId());
        }
        if (!(tag.getName() == null  || "".equals(tag.getName()))) {
            criteria.andNameLike("%" + tag.getName() + "%");
        }
        PageHelper.startPage(page, limit);
        List<Tag> tagList = tagMapper.selectByExampleAndGetCategory(tagExample);
        return new PageInfo<Tag>(tagList);
    }

    public Tag getTagById(Integer id) {
        return tagMapper.selectByPrimaryKey(id);
    }

    public Boolean insertTag(Tag tag) {
        int insert = tagMapper.insert(tag);
        return insert>0?true:false;
    }

    public Boolean updateTagById(Tag tag) {
        int update = tagMapper.updateByPrimaryKey(tag);
        return update>0?true:false;
    }

    public Set<String> getTagList() {
        List<String> tagList = questionMapper.selectTag();
        Set<String> tagSet = new HashSet<>();
        for (String tags : tagList) {
            String[] tagChar = StringUtils.split(tags, ",");
            for (String tag : tagChar) {
                tagSet.add(tag);
            }
        }
        return tagSet;
    }
}
